package ex.hql.config;

import ex.hql.pojo.Admin;
import ex.hql.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/*
 *@program:yeb
 *@author: hql
 *@Time: 2021/8/20  15:44
 *@description: UserDetaileService
 */
@Component
public class MyUserDetailService implements UserDetailsService {

    @Autowired
    private IAdminService adminService;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Admin admin = adminService.getAdminByUserName(username);
        if(admin!=null){
            admin.setRoles(adminService.getRoles(admin.getId()));
            return admin;
        }
        throw new UsernameNotFoundException("用户名或者密码不正确");
    }
}
